/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package proyectocallcentergrupo6;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gaston
 */
public class Admin extends javax.swing.JInternalFrame {

    /**
     * Creates new form Admin
     */
    public Admin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        sda = new javax.swing.JLabel();
        daca = new javax.swing.JLabel();
        wqdwq = new javax.swing.JLabel();
        dawqd = new javax.swing.JLabel();
        txtid = new javax.swing.JTextField();
        txtname = new javax.swing.JTextField();
        txtemail = new javax.swing.JTextField();
        txtfono = new javax.swing.JTextField();
        eqw = new javax.swing.JLabel();
        combociudad = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        cmdNuevo = new javax.swing.JButton();
        cmdRegistro = new javax.swing.JButton();
        cmdActualizar = new javax.swing.JButton();
        cmdEliminar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtapellido = new javax.swing.JTextField();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList1);

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 389, Short.MAX_VALUE)
        );

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);

        sda.setText("Id:");

        daca.setText("Nombre:");

        wqdwq.setText("Email:");

        dawqd.setText("Fono:");

        eqw.setText("Ciudad:");

        combociudad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione una opción", "Ancud", "Antofagasta", "Arica", "Calbuco", "Castro", "Chillan", "Concepción", "Copiapo", "Coquimbo", "Dalcahue", "Frutillar", "Iquique", "Osorno", "Puerto Montt", "Puerto Varas", "Punta Arenas", "Quellón", "Santiago", "Temuco", "Valdivia", "Viña Del Mar" }));

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        cmdNuevo.setText("Nuevo Registro");
        cmdNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuevoActionPerformed(evt);
            }
        });

        cmdRegistro.setText("Agregar Registro");
        cmdRegistro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRegistroActionPerformed(evt);
            }
        });

        cmdActualizar.setText("Actualizar Registro");
        cmdActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdActualizarActionPerformed(evt);
            }
        });

        cmdEliminar.setText("Eliminar Registro");
        cmdEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(cmdNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(cmdRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42)
                .addComponent(cmdActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                .addComponent(cmdEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        jLabel1.setText("Apellido: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 860, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(sda)
                            .addComponent(wqdwq)
                            .addComponent(dawqd))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtemail, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtfono, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(daca)
                                .addGap(15, 15, 15)
                                .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(60, 60, 60)
                                .addComponent(jLabel1)
                                .addGap(22, 22, 22)
                                .addComponent(txtapellido, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(eqw)
                                .addGap(18, 18, 18)
                                .addComponent(combociudad, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(sda)
                                .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(daca)
                                .addComponent(txtname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(txtapellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(wqdwq)
                            .addComponent(txtemail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(9, 9, 9)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(dawqd)
                                    .addComponent(txtfono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(eqw)
                                    .addComponent(combociudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(14, 14, 14))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuevoActionPerformed
        nuevo();
        
    }//GEN-LAST:event_cmdNuevoActionPerformed

    private void cmdRegistroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRegistroActionPerformed
        // TODO add your handling code here:
            String id   = txtid.getText();
            String name    = txtname.getText();
            String apellido = txtapellido.getText();
            String email    = txtemail.getText();
            String fono    = txtfono.getText();
            String ciudad   = (String) combociudad.getSelectedItem();
            p.agregar(id, name,apellido, ciudad, email,fono);
            s.agregarServicio(id);
            k.agregarCanal(id);
            JOptionPane.showMessageDialog(this, "Atencion: Modificar servicios y Canales en modificar servicios");
            updateTabla();
    }//GEN-LAST:event_cmdRegistroActionPerformed

    private void cmdActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdActualizarActionPerformed
        // TODO add your handling code here:
        if(fila > -1){
            String id   = txtid.getText();
            String name    = txtname.getText();
            String apellido = txtapellido.getText();
            String email    = txtemail.getText();
            String fono    = txtfono.getText();
            String ciudad   = (String) combociudad.getSelectedItem();
            p.modificar(id, name,apellido, email, fono, ciudad);
            updateTabla();
        }
    }//GEN-LAST:event_cmdActualizarActionPerformed

    private void cmdEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEliminarActionPerformed
        // TODO add your handling code here:
        if(fila > -1){
            String idcliente = txtid.getText();
            k.eliminar(idcliente);
            s.eliminarServicio(idcliente);
            p.eliminar(idcliente);
            updateTabla();
            fila = -1;
        }
    }//GEN-LAST:event_cmdEliminarActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
         Object[][] dtPer;        
        String[] columNames = {"IDCLIENTE","NOMBRE","APELLIDO","CIUDAD","EMAIL","TELEFONO"};     
        dtPer = p.getDatos();
        DefaultTableModel datos;
        datos = new DefaultTableModel(dtPer,columNames);                        
        tabla.setModel(datos); 
        updateTabla();
      
    }//GEN-LAST:event_formInternalFrameOpened

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        fila = tabla.rowAtPoint(evt.getPoint());
        if(fila > -1){
            txtid.setText(String.valueOf(tabla.getValueAt(fila, 0)));
            txtname.setText(String.valueOf(tabla.getValueAt(fila, 1)));
            txtapellido.setText(String.valueOf(tabla.getValueAt(fila, 2)));
            cambiarCiudad(String.valueOf(tabla.getValueAt(fila, 3)));
//            combociudad.setSelectedItem(String.valueOf(tabla.getValueAt(fila, 3)));
            txtemail.setText(String.valueOf(tabla.getValueAt(fila, 4)));
            txtfono.setText(String.valueOf(tabla.getValueAt(fila, 5)));      
        }
    }//GEN-LAST:event_tablaMouseClicked
    private void cambiarCiudad(String C){
        switch (C) {
                case "Ancud": combociudad.setSelectedIndex(1);break;
                case "Antofagasta": combociudad.setSelectedIndex(2);break;
                case "Arica": combociudad.setSelectedIndex(3);break;
                case "Calbuco": combociudad.setSelectedIndex(4);break;
                case "Castro": combociudad.setSelectedIndex(5);break;
                case "Chillan": combociudad.setSelectedIndex(6);break;
                case "Concepción": combociudad.setSelectedIndex(7);break;
                case "Copiapo": combociudad.setSelectedIndex(8);break;
                case "Coquimbo": combociudad.setSelectedIndex(9);break;
                case "Dalcahue": combociudad.setSelectedIndex(10);break;
                case "Frutillar": combociudad.setSelectedIndex(11);break;
                case "Iquique": combociudad.setSelectedIndex(12);break;
                case "Osorno": combociudad.setSelectedIndex(13);break;
                case "Puerto Montt": combociudad.setSelectedIndex(14);break;
                case "Puerto Varas": combociudad.setSelectedIndex(15);break;
                case "Punta Arenas": combociudad.setSelectedIndex(16);break;
                case "Quellón": combociudad.setSelectedIndex(17);break;
                case "Santiago": combociudad.setSelectedIndex(18);break;
                case "Temuco": combociudad.setSelectedIndex(19);break;
                case "Valdivia": combociudad.setSelectedIndex(20);break;
                case "Viña Del Mar": combociudad.setSelectedIndex(21);break;
            }
    }
Cliente p = new Cliente();   
Servicio s = new Servicio();
Canales k = new Canales();
    Object[][] dtPer; 
    int fila = -1;
    
    
    public Usuario getC() {
        return c;
    }
    Usuario c = null;
    public void setC(Usuario c) {
        this.c = c;
    }
    
     private void updateTabla(){             
        String[] columNames = {"IDCLIENTE","NOMBRE","APELLIDO","CIUDAD","EMAIL","TELEFONO"};  
       
        dtPer = p.getDatos();
      
        DefaultTableModel datos = new DefaultTableModel(dtPer,columNames);                        
        tabla.setModel(datos); 
    }
         private void nuevo(){
        txtid.setText("");
        txtname.setText("");
        txtapellido.setText("");
        txtemail.setText("");
        txtfono.setText("");
        combociudad.setSelectedItem("");
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdActualizar;
    private javax.swing.JButton cmdEliminar;
    private javax.swing.JButton cmdNuevo;
    private javax.swing.JButton cmdRegistro;
    private javax.swing.JComboBox combociudad;
    private javax.swing.JLabel daca;
    private javax.swing.JLabel dawqd;
    private javax.swing.JLabel eqw;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel sda;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txtapellido;
    private javax.swing.JTextField txtemail;
    private javax.swing.JTextField txtfono;
    private javax.swing.JTextField txtid;
    private javax.swing.JTextField txtname;
    private javax.swing.JLabel wqdwq;
    // End of variables declaration//GEN-END:variables
}
